//
//  QKBaseAlertManager.m
//  QKiOS
//
//  Created by lushitong on 2018/12/14.
//  Copyright © 2018年 qingka. All rights reserved.
//

#import "QKBaseAlertManager.h"

@implementation QKBaseAlertManager

/**
 * 移除所有的JKAlertView
 * 本质是发一个通知给当前所有的JKAlertView，让其执行消失操作
 * 如果想要移除后再次弹框，注意一定要先执行移除再执行弹框
 */
+ (void)dismissAll{
	
	JKAlertView.dismissAll();
}

/**
 * 根据key移除JKAlertView
 * 本质是发一个通知给当前所有的JKAlertView，key一致则让其执行消失操作
 * 如果想要移除后再次弹框，注意一定要先执行移除再执行弹框
 */
+ (void)dismissForKey:(NSString *)key{
	
	JKAlertView.dismissForKey(key);
}

+ (void)setAllCommonConfig:(JKAlertView *)alertView{
	
	// 通用背景
	[self setCommonBackground:alertView];
	
	// 通用关闭按钮
	[self setCommonCloseButton:alertView];
	
	// 通用动画
	[self setCommonAnimationWithAlertView:alertView];
}

+ (void)setCommonBackground:(JKAlertView *)alertView{
	
	alertView.setBackGroundView(^UIView *{
		
		// 背景
		UIView *backgroundView = [[UIView alloc] init];
		
		backgroundView.backgroundColor = [UIColor whiteColor];
		
		return backgroundView;
	});
}

+ (void)setCommonCloseButton:(JKAlertView *)alertView{
	
	alertView.setPlainCloseButtonConfig(^(UIButton *closeButton) {
		
		// 关闭按钮
		[closeButton setTitle:nil forState:(UIControlStateNormal)];
		[closeButton setImage:[UIImage imageNamed:@"alert_close"] forState:(UIControlStateNormal)];
		
		// 约束closeButton
		closeButton.translatesAutoresizingMaskIntoConstraints = NO;
		[closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
			make.top.mas_equalTo(0);
			make.right.mas_equalTo(0);
			make.width.height.mas_equalTo(34);
		}];
	});
}

#pragma mark
#pragma mark - 设置通用动画

+ (void)setCommonAnimationWithAlertView:(JKAlertView *)alertView{
	
	//    alertView.setshide
	
	//    alertView.setCustomPlainTitleView(YES, ^UIView *(JKAlertView *view) {
	//
	//        return [[UIView alloc] initWithFrame:(CGRectMake(0, 0, 0, 100))]
	//
	//    });
	
#ifdef DEBUG
	alertView.enableDeallocLog(YES);
#endif
	
	alertView.setCustomShowAnimationBlock(^(JKAlertView *view, UIView *animationView) {
		
		view.window.userInteractionEnabled = YES;
		
		animationView.transform = CGAffineTransformMakeScale(0.3, 0.3);
		
		[UIView animateWithDuration:1.0 delay:0.0 usingSpringWithDamping:1.0 initialSpringVelocity:15.0 options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction animations:^{
			
			animationView.transform = CGAffineTransformMakeScale(1.0, 1.0);
			
		} completion:^(BOOL finished) {
			
			view.showAnimationDidComplete();
		}];
	});
}

/**
 * 添加自定义确认按钮
 * isAutoDimiss : 点击确认是否自动消失
 * isMiddleButton : 按钮是否处于中间
 UI图的关系，默认按钮在action的顶部，但也有些需要放在中间，使顶部也有一些间距
 */
+ (void)addVerifyActionWithAlertView:(JKAlertView *)alertView
						 verifyTitle:(NSString *)verifyTitle
						isAutoDimiss:(BOOL)isAutoDimiss
					  isMiddleButton:(BOOL)isMiddleButton
						verifyAction:(void(^)(JKAlertAction *action))verifyAction{
	
	// 按钮放在中间，即按钮上下有17间距
	if (isMiddleButton) {
		
		alertView.addAction(JKAlertAction.action(nil, JKAlertActionStyleDefault, nil).setCustomView(^UIView *(JKAlertAction *action) {
			
			return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 10)];
			
		}).setSeparatorLineHidden(YES)).addAction(JKAlertAction.action(nil, JKAlertActionStyleDefault, nil).setCustomView(^UIView *(JKAlertAction *action) {
			
			return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 7)];
			
		}).setSeparatorLineHidden(YES));
	}
	
	alertView.addAction(JKAlertAction.action(nil, JKAlertActionStyleDefault, nil).setSeparatorLineHidden(YES).setCustomView(^UIView *(JKAlertAction *action) {
		
		return [self createVerifyActionViewWithAction:action verifyTitle:verifyTitle isAutoDimiss:isAutoDimiss verifyAction:verifyAction];
	}));
}

+ (UIView *)createVerifyActionViewWithAction:(JKAlertAction *)action
								 verifyTitle:(NSString *)verifyTitle
								isAutoDimiss:(BOOL)isAutoDimiss
								verifyAction:(void(^)(JKAlertAction *action))verifyAction{
	
	UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 57)];
	
	UIButton *verifyButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
	verifyButton.titleLabel.font = [UIFont systemFontOfSize:16];
	[verifyButton setTitle:verifyTitle forState:(UIControlStateNormal)];
	[verifyButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
	[verifyButton setBackgroundImage:[self commonGradientImage] forState:(UIControlStateNormal)];
	
	[view addSubview:verifyButton];
	
	[verifyButton JKAlertX_addClickOperation:^(id control) {
		
		if (isAutoDimiss) {
			
			action.alertView.dismiss();
		}
		
		!verifyAction ? : verifyAction(action);
	}];
	
	// 约束verifyButton
	verifyButton.translatesAutoresizingMaskIntoConstraints = NO;
	[verifyButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.mas_equalTo(0);
		make.centerX.mas_equalTo(view);
		make.width.mas_equalTo(250);
		make.height.mas_equalTo(40);
	}];
	
	return view;
}

static UIImage *commonGradientImage_;

+ (UIImage *)commonGradientImage{
	
	if (!commonGradientImage_) {
		
		commonGradientImage_ = [[UIImage createImageWithSize:CGSizeMake(250, 40) gradientColors:@[[UIColor colorWithHexString:@"#DC4BD2"], [UIColor colorWithHexString:@"#5C77FA"]] percentage:@[@0, @1] gradientType:(GradientFromLeftToRight)] roundedCornerImageWithCornerRadius:20];
	}
	
	return commonGradientImage_;
}

+ (UIImage *)commonGradientImageWithSize:(CGSize)size{
	
	if (CGSizeEqualToSize(size, CGSizeMake(250, 40))) {
		
		return [self commonGradientImage];
	}
	
	return [[UIImage createImageWithSize:size gradientColors:@[[UIColor colorWithHexString:@"#DC4BD2"], [UIColor colorWithHexString:@"#5C77FA"]] percentage:@[@0, @1] gradientType:(GradientFromLeftToRight)] roundedCornerImageWithCornerRadius:MIN(size.width, size.height) * 0.5];
}
@end
